# /usr/bin/env python
# -*- coding: UTF-8 -*-
import pyttsx3

engine = pyttsx3.init()  # object creation

""" RATE"""
rate = engine.getProperty('rate')  # getting details of current speaking rate
print(rate)  # printing current voice rate
engine.setProperty('rate', 100)  # setting up new voice rate

"""VOLUME"""
volume = engine.getProperty('volume')  # getting to know current volume level (min=0 and max=1)
print(volume)  # printing current volume level
engine.setProperty('volume', 1.0)  # setting up volume level  between 0 and 1

"""VOICE"""
voices = engine.getProperty('voices')  # getting details of current voice
for voice in voices:
    print("Voice:")
    print(" - ID: %s" % voice.id)
    print(" - Name: %s" % voice.name)
    print(" - Languages: %s" % voice.languages)
    print(" - Gender: %s" % voice.gender)
    print(" - Age: %s" % voice.age)
engine.setProperty('voice', 'zh') #开启支持中文
# engine.setProperty('voice', voices[0].id)  #changing index, changes voices. o for male
engine.setProperty('voice', voices[1].id)  # changing index, changes voices. 1 for female

# 把刚才代码输出的语音包ID信息储存起来
zh_voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_HUIHUI_11.0"
# 用语音包ID来配置engine
engine.setProperty('voice', zh_voice_id)
# engine.say('''xxx''')
# engine.say('My current speaking rate is ' + str(rate))
# engine.runAndWait()
# engine.stop()

"""Saving Voice to a file"""
# On linux make sure that 'espeak' and 'ffmpeg' are installed
with open('D:\data1\小说类\武侠玄幻\金庸全集\新修版\碧血剑（世纪新修版）.txt','r', encoding='utf-8')as rf:
    novel_str = rf.read()
engine.save_to_file(novel_str, '碧血剑.mp3')
engine.runAndWait()
engine.stop()